package com.fashion.web.actions.gallery;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.fashion.model.News;
import com.fashion.model.gallery.Album;
import com.fashion.model.gallery.GalleryColumn;
import com.fashion.model.gallery.Photo;
import com.fashion.service.NewsService;
import com.fashion.service.gallery.AlbumService;
import com.fashion.service.gallery.GalleryColumnService;
import com.fashion.service.gallery.PhotoService;
import com.fashion.utils.StringUtils;
import com.fashion.utils.UrlKeyword;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Namespace("/gallery")
public class AlbumAction extends ActionSupport {
	private Album album;
	private List<Photo> photos;
	private List<GalleryColumn> parents;
	private List<GalleryColumn> children;
	private List<Album> recommendations;
	private List<News> newsList;
	private List<Album> hots;
	@Resource(name = "photoService")
    private PhotoService photoService;
	@Resource(name = "albumService")
	private AlbumService albumService;
	@Resource(name = "galleryColumnService")
    private GalleryColumnService galleryColumnService;
	@Resource(name = "newsService")
	private NewsService newsService;
	private List<UrlKeyword> keywords = new ArrayList<UrlKeyword>();
	
	public List<UrlKeyword> getKeywords() {
		return keywords;
	}

	public void setAlbum(Album album) {
        this.album = album;
    }

    public Album getAlbum() {
		return album;
    }

    public List<Album> getRecommendations() {
		return recommendations;
	}

	public void setRecommendations(List<Album> recommendations) {
		this.recommendations = recommendations;
	}

	public List<Photo> getPhotos() {
        return photos;
    }

    public void setPhotos(List<Photo> photos) {
        this.photos = photos;
    }
    
    
    public List<News> getNewsList() {
		return newsList;
	}

	@Action(value = "album", results = {@Result(name = SUCCESS, location = "album.jsp")})
	public String index() throws Exception {
    	albumService.click(album.getId());
    	album = albumService.findById(album.getId());
    	List<GalleryColumn> galleryColumns = galleryColumnService.format(galleryColumnService.findAll());
    	GalleryColumn column =  galleryColumnService.findColumnFromTree(galleryColumns, album.getColumn());
    	parents = galleryColumnService.getAllParents(column);
    	children = galleryColumnService.getAllChildren(column);
    	
        photos = photoService.findByAlbum(album.getId());
        newsList = newsService.searchSuggestNews("", "", 1, 4);
        hots = albumService.find(null, null, "clickNumber", 0, Integer.MAX_VALUE);
        
        List<String> rawKeywords = StringUtils.splitToList(album.getKeywords(), " ");
        for (String keyword : rawKeywords) {
            String keywordWithoutSpace = keyword.replace("$", " ");
            keywords.add(StringUtils.makeUrlKeyword(keywordWithoutSpace));
        }
		return SUCCESS;
	}

     @Action(value = "home", results = {@Result(name = SUCCESS, location = "home.jsp")})
	public String home() throws Exception {
		return SUCCESS;
	}

	public List<GalleryColumn> getParents() {
		return parents;
	}

	public List<GalleryColumn> getChildren() {
		return children;
	}

	public List<Album> getHots() {
		return hots;
	}
     
     
}
